package servlet;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONObject;

import connection.DbConnection;

import response.DeviceInterface;

import model.PollImpl;
import model.TimerImple;
import bean.Poll;
import bean.PollUser;
import bean.Question;
import model.EmailingSystem;

public class timerServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8381668085609703186L;

	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		
		HttpSession session=request.getSession(true);
    	int qID = Integer.parseInt(request.getParameter("questionid"));
		Question q = getQuestion(qID);
    	TimerImple ti = new TimerImple();
    	int[] ans = ti.getCurrentTimer(q.getQuestionID());
    	
    	JSONObject obj = new JSONObject();
		obj.put("timer", ans[0]);
		obj.put("status", ans[1]);
		response.getWriter().print(obj);
		response.getWriter().flush();//*cross fingers*
		
		//RequestDispatcher rd=request.getRequestDispatcher("timer.jsp");
		//rd.forward(request, response);
	}
	
	public static Question getQuestion(int qID){
		DbConnection conn = new DbConnection();
    	Question question = new Question();
    	String sql = "SELECT * FROM Question WHERE QuestionID="+qID;
    	System.out.println("getQuestion: qID = "+qID);
    	ResultSet rs ;
    	try{
    		PreparedStatement ps = conn.getConn().prepareStatement(sql);
    		rs = ps.executeQuery();
    		try{
	    		if(rs.next()){
	    			System.out.println("over here in getQuestion!");
	    			question.setQuestionID(rs.getInt("QuestionID"));
	    			question.setText(rs.getString("Text"));
	    			question.setType(rs.getString("Type"));
	    			question.setStatus(rs.getInt("Status"));
	    			question.setCharStyles(rs.getString("ChartStyles"));
	    			question.setChartColor(rs.getString("ChartColor"));
	    			question.setColor(rs.getString("Color"));
	    			question.setTimer(rs.getString("Timer"));
	    			question.setCurrentTimer(rs.getInt("CurrentTimer"));
	    			question.setFont(rs.getString("Font"));
	    			question.setCAI(rs.getString("CAI"));
	    			question.setHighest(rs.getDouble("Highest"));
	    			question.setLowest(rs.getDouble("Lowest"));
	    			question.setFontSize(rs.getInt("FontSize"));
	    			question.setResCounter(rs.getString("ResCounter"));
	    			question.setNumberOnly(rs.getInt("NumberOnly"));
	    			question.setNumberOfAnswer(rs.getInt("NumberOfAnswer"));
	    			question.setCorrect1(rs.getString("Correct1"));
	    			question.setCorrect2(rs.getString("Correct2"));
	    			question.setCorrect3(rs.getString("Correct3"));
	    			question.setCorrect4(rs.getString("Correct4"));
	    			question.setCorrect5(rs.getString("Correct5"));
	    			System.out.println(question.getText());
	    		}
    		}catch(SQLException e){
    			System.out.println("sadsad");
    		}
    		
    	}catch(SQLException e){
    		
    		System.out.println("getQuestion failed!");
    		e.printStackTrace();
    	}
    	return question;
    }
	
}
